package custom;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;

import java.util.List;

import adapter.ShoppingGoodsLvAdapter;
import adapter.ShoppingSortLvAdapter;
import app.LingShiJiaApp;
import utils.ShoppingCart;

/**
 * Created by Mr.xingkong on 2016/1/16.
 */
public class ShoppingItem extends LinearLayout
{
    Context contex;


    ListView goods_lv;
    List<ShoppingCart> data;
    ShoppingGoodsLvAdapter adapter;
    ShoppingItem_headView headView;
    ShoppingItem_footView footView;
    public ShoppingItem(Context context)
    {
        super(context);
        this.contex = context;
        initView();
    }
    public ShoppingItem(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.contex = context;
        initView();
    }

    private void initView()
    {
        View view = LayoutInflater.from(contex).inflate(R.layout.shoppingitem_layout,this);
         headView = new ShoppingItem_headView(contex);
         footView = new ShoppingItem_footView(contex);
         goods_lv = (ListView) view.findViewById(R.id.goods_lv);
        goods_lv.addHeaderView(headView);
        goods_lv.addFooterView(footView);
         adapter = new ShoppingGoodsLvAdapter(contex);
        goods_lv.setAdapter(adapter);
    }

    public void setData(List<ShoppingCart> data)
    {
        this.data = data;
        adapter.setData(data);
        headView.setData(data.get(0).getStore());
        buildListView();
    }

    private void buildListView()
    {
        int totalHeight = 0;
        /*for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, goods_lv);
            *//*int w = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0,
                    View.MeasureSpec.UNSPECIFIED);

            listItem.measure(w, h);*//*
            listItem.measure(0, 0);
            Log.d("height", "buildListView() returned: " + listItem.getMeasuredHeight());
            //totalHeight +=Dp2Px(contex, listItem.getMeasuredHeight() + goods_lv.getDividerHeight()) ;
            totalHeight +=(listItem.getMeasuredHeight() + goods_lv.getDividerHeight());
        }*/
        View listItem = adapter.getView(0, null, goods_lv);
        listItem.measure(0, 0);
        //Log.d("height", "buildListView() returned: " + LingShiJiaApp.getScale());
       totalHeight = data.size()*listItem.getMeasuredHeight() + 250;
        ViewGroup.LayoutParams layoutParams = goods_lv.getLayoutParams();
        layoutParams.width = LayoutParams.FILL_PARENT;
        layoutParams.height = totalHeight;
        goods_lv.setLayoutParams(layoutParams);

    }
    public int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
